﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Common;
using System.Linq;
using System.Text;
using System.Web;
using Microsoft.Practices.EnterpriseLibrary.Caching;
using Microsoft.Practices.EnterpriseLibrary.Caching.Expirations;

namespace VideoNetApplication.Common.Service
{
    public static class MembershipService
    {
        /// <summary>
        /// 获取学员信息 并将学员信息放入缓存
        /// </summary>
        /// <param name="userId"></param>
        /// <param name="schoolId"></param>
        /// <returns></returns>
        public static V_user GetUser(int userId, int schoolId)
        {
            ICacheManager userCache = CacheFactory.GetCacheManager();
            string UsCacheID = MembershipService.GenerateName(userId, schoolId) + "@U";
            V_user user = userCache.GetData(UsCacheID) as V_user;
            if (user == null)
            {
                //var vb = VDataBase.CreateDatabase();
                //string sql = "SELECT * FROM V_user WHERE ID=" + userId + " and SchoolID=" + schoolId;
                //using (DbCommand dbCommand = vb.GetSqlStringCommand(sql))
                //{
                //    DataSet ds = vb.ExecuteDataSet(dbCommand);
                //    var query = from s in ds.Tables[0].AsEnumerable()
                //                select s;
                //    user = query.Select(e => new V_user()
                //    {
                //        ID = e.Field<int>("ID"),
                //        SchoolID = e.Field<int>("SchoolID"),
                //        create_time = e.Field<DateTime>("create_time"),
                //        IsAvailable = e.Field<bool>("IsAvailable"),
                //        IsBuyingAccount = e.Field<bool>("IsBuyingAccount"),
                //        update_time = e.Field<DateTime>("update_time"),
                //        VU_email = e.Field<string>("VU_email"),
                //        VU_loginname = e.Field<string>("VU_loginname"),
                //        VU_mobile = e.Field<string>("VU_mobile"),
                //        VU_Password = e.Field<string>("VU_Password"),
                //        VU_realname = e.Field<string>("VU_realname")
                //    }).FirstOrDefault();


                //}

                using (EVDBEntities edm=new EVDBEntities())
                {
                    var query = edm.V_user.Where(s => s.ID == userId && s.SchoolID == schoolId);
                    user = query.FirstOrDefault();

                }


                userCache.Add(UsCacheID, user, CacheItemPriority.High, null, new SlidingTime(TimeSpan.FromMinutes(10)));


            }
            return user;
        }

        public static string GenerateName(int userId, int schoolId)
        {
            return userId + "@" + schoolId.ToString();
        }

        public static V_user FlushUser(int userId, int schoolId)
        {
            ICacheManager userCache = CacheFactory.GetCacheManager();
            string UsCacheID = GenerateName(userId, schoolId) + "@U";

            if (userCache.Contains(UsCacheID))
                userCache.Remove(UsCacheID);

            return MembershipService.GetUser(userId, schoolId);
        }

        public static V_user GetStudentUser()
        {
            string[] userName = GetSplitLogName();
            if (userName.Length > 1)
            {
                return GetUser(userName[0] == "" ? 0 : int.Parse(userName[0]),
                               userName[1] == "" ? 1 : int.Parse(userName[1]));
            }else
            {
                return null;
            }
        }

        /// <summary>
        /// 分割学员缓存信息
        /// </summary>
        /// <returns></returns>
        private static string[] GetSplitLogName()
        {
            string[] userName = HttpContext.Current.User.Identity.Name.Split('@');
            return userName;
        }

        public static V_user GetUser()
        {
            return GetStudentUser();

        }
    }
}
